package com.ruoyi.file.controller;

import com.ruoyi.blog.core.controller.BaseController;
import com.ruoyi.blog.core.domain.AjaxResult;
import com.ruoyi.blog.utils.StringUtils;
import com.ruoyi.file.domain.po.FileInfo;
import com.ruoyi.file.service.FileService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 依梦
 * @Date: 2024/10/6
 * @Description: FileController
 */
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {

    @Resource
    private FileService fileService;

    /**
     * 获取文件列表
     */
    @GetMapping("/list")
    public AjaxResult getList(FileInfo info) {
        info.setUserId(getUserId());
        List<FileInfo> list = fileService.getList(info);
        return AjaxResult.success(list);
    }

    // 上传文件
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam(value = "file") MultipartFile[] files, String dirIds) {
        return fileService.upload(files, dirIds);
    }

    // 新建文件夹
    @PostMapping("/addFolder")
    public AjaxResult addFolder(@RequestBody FileInfo info) {
        if (fileService.addFolder(info)) {
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * 项目文件上传
     *
     * @param projectId 项目id
     * @param columnsId 栏目id
     * @param files     要上传的文件 id
     * @return 上传结果
     */
    @PostMapping("/uploadProject/{projectId}/{issueId}/{columnsId}")
    public AjaxResult uploadProject(@PathVariable("projectId") Long projectId, @PathVariable("issueId") Long issueId, @PathVariable("columnsId") Long columnsId, MultipartFile[] files) {
        return AjaxResult.success(fileService.uploadProject(projectId, columnsId, issueId, files));
    }


    /**
     * 删除文件
     *
     * @param id 文件id
     * @return 是否删除成功
     */
    @DeleteMapping("/deleteFile")
    public AjaxResult deleteProjectFile(Long id) {
        if(StringUtils.isNull(id)){
            throw new RuntimeException("id 为 null");
        }
        return toAjax(fileService.deleteFileProById(id));
    }
}
